class Solution:
    def floodFill(self, image: List[List[int]], sr: int, sc: int, newColor: int) -> List[List[int]]:
        if image[sr][sc] == newColor:
            return image
        direction = [(0,-1),(0,1),(-1,0),(1,0)]
        color = image[sr][sc]
        def fill(i,j):
            if image[i][j] == color:
                image[i][j] = newColor
                for dx,dy in direction:
                    x = i + dx
                    y = j + dy
                    if 0 <= x < len(image) and 0 <= y <len(image[0]):
                        fill(x,y)
        fill(sr,sc)
        return image